package cn.lyd.utils.redis;

import cn.lyd.enums.MyEnum;
import cn.lyd.exceptions.MyselfException;
import cn.lyd.utils.asserts.AssertUtil;
import cn.lyd.vo.AjaxResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * redis工具类
 */
@Component
public class RedisClient {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private AssertUtil assertUtil;


    /**
     * 存放普通键值对 并设置缓存时间
     * @param key 键
     * @param value 值
     * @param time 存放时间
     * @return
     */
    public AjaxResults set(String key , Object value , long time) {
        try {

            if (time>0) {
                this.redisTemplate.opsForValue().set(key,value,time,TimeUnit.SECONDS);
            } else {
                redisTemplate.opsForValue().set(key,value);
            }
            return AjaxResults.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResults.fail(null);
        }finally {
            System.out.println("测试完毕");
        }

    }


    /**
     * 判断key是否存在
     * @param key
     * @return
     */
    public boolean hasKey(String key){
        try {
            if (assertUtil.StringNotNull(key)) {
                Boolean aBoolean = redisTemplate.hasKey(key);
                return aBoolean;
            }else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }



    /**
     * 根据key获取value
     * @param key
     * @return
     */
    public Object get(String key) {
        try {
            if (assertUtil.StringNotNull(key)) {
                Object o = redisTemplate.opsForValue().get(key);
                return o;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            System.out.println("测试完毕");
        }

    }


    /**
     * 获取剩余时间
     * @param key
     * @return
     */
    public Long getExpire(String key){
        try {
            if (assertUtil.StringNotNull(key)&&this.hasKey(key)) {
                Long expire = redisTemplate.getExpire(key);
                return expire;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 删除key
     * @param key
     * @return
     */
    public AjaxResults del(String key){
        try {
            if (assertUtil.StringNotNull(key)&&this.hasKey(key)) {
                  redisTemplate.delete(key);
                  return AjaxResults.EnumAjaxResults(MyEnum.REDIS_CACHE_DEL_SUCCESS,null);
            }else {
                throw new MyselfException(MyEnum.REDIS_CACHE_KEY_NOTNULL.getMsg());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResults.newAjaxResults(null,e.getMessage(),null);
        }
    }



}
